﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YouDaoNotesCodes.CSharp_highLevel
{
    /// <summary>
    /// 委托类型-配合lamdba表达式
    /// </summary>
    class @delegate
    {
        #region ① delegate 
        public delegate int MethodDelegate(int x, int y);

        public void delegateType()
        {
            MethodDelegate method = new MethodDelegate(Add);//=delegate(12,12);

            var res = method(12, 12);//method.Invoke(1,2);
            Console.WriteLine(res);
            Console.ReadKey();
        }
        private int Add(int x, int y)
        {
            return x + y;
        }
        #endregion

        #region② Action
        public void ActionType()
        {
            Test<string>(Action, "Hello World!");
            Test<int>(Action, 1000);
            Test<string>(p => { Console.WriteLine("{0}", p); }, "Hello World");//使用Lambda表达式定义委托
            Console.ReadKey();
        }
        public static void Test<T>(Action<T> action, T p)
        {
            action(p);
        }
        private static void Action(string s)
        {
            Console.WriteLine(s);
        }
        private static void Action(int s)
        {
            Console.WriteLine(s);
        }

        #endregion

        #region ③ Func
        public void FuncType()
        {
            Console.WriteLine(Test<int, int>(Fun, 100, 200));
            Console.ReadKey();
        }

        public static int Test<T1, T2>(Func<T1, T2, int> func, T1 a, T2 b)
        {
            return func(a, b);
        }
        private static int Fun(int a, int b)
        {
            return a + b;
        }

        #endregion

        #region ④ predicate
        　//　泛型委托：表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用，用于在集合中搜索元素。
        public void predictType()
        {

            Predicate<int> pred = (x) => { return false; };
            Console.WriteLine(pred(2));
        }

        #endregion
    }
}
